/**
 * Created by fanweihua on 2016/11/30.
 * 操作左边菜单
 * @type {{$container: (any), _pageStack: Array, _configs: Array, _pageAppend: pageManager._pageAppend, _defaultPage: null, _pageIndex: number, go: pageManager.go, push: pageManager.push, back: pageManager.back, _back: pageManager._back, _findInStack: pageManager._findInStack, _find: pageManager._find, _bind: pageManager._bind, _go: pageManager._go, _operation: pageManager._operation}}
 */
let pageManager = {
    $container: $('#container'),
    _pageStack: [],
    _configs: [],
    _pageAppend: function () {
    },
    _defaultPage: null,
    _pageIndex: 1,
    go: function (to) {
        var config = this._find('name', to);
        if (!config) {
            return;
        }
        location.hash = config.url;
    },
    push: function (config) {
        this._configs.push(config);
        return this;
    },
    back: function () {
        history.back();
    },
    _back: function (config) {
        this._pageIndex--;

        var stack = this._pageStack.pop();
        if (!stack) {
            return;
        }

        var url = location.hash.indexOf('#') === 0 ? location.hash : '#';
        var found = this._findInStack(url);
        if (!found) {
            var html = $(config.template).html();
            var $html = $(html).addClass('js_show').addClass(config.name);
            $html.insertBefore(stack.dom);

            if (!config.isBind) {
                this._bind(config);
            }

            this._pageStack.push({
                config: config,
                dom: $html
            });
        }

        stack.dom.addClass('slideOut').on('animationend webkitAnimationEnd', function () {
            stack.dom.remove();
        });

        return this;
    },
    _findInStack: function (url) {
        var found = null;
        for (var i = 0, len = this._pageStack.length; i < len; i++) {
            var stack = this._pageStack[i];
            if (stack.config.url === url) {
                found = stack;
                break;
            }
        }
        return found;
    },
    _find: function (key, value) {
        var page = null;
        for (var i = 0, len = this._configs.length; i < len; i++) {
            if (this._configs[i][key] === value) {
                page = this._configs[i];
                break;
            }
        }
        return page;
    },
    _bind: function (page) {
        var events = page.events || {};
        for (var t in events) {
            for (var type in events[t]) {
                this.$container.on(type, t, events[t][type]);
            }
        }
        page.isBind = true;
    },
    _go: function () {
        var html = $('#tpl_home').html();
        var $html = $(html).addClass('slideIn').addClass('home');
        $html.on('animationend webkitAnimationEnd', function () {
            $html.removeClass('slideIn').addClass('js_show');
        });
        this.$container.append($html);
        this._operation();//操作
    },
    /**
     * 操作
     * @private
     */
    _operation: function () {
        var winH = $(window).height();
        var categorySpace = 10;

        $('.js_item').on('click', function () {
            var id = $(this).data('id');
            pageManager.go(id);
        });
        $('.js_category').on('click', function () {
            var $this = $(this),
                $inner = $this.next('.js_categoryinner'),
                $page = $this.parents('.page'),
                $parent = $(this).parent('li');
            var innerH = $inner.data('height');
            if (!innerH) {
                $inner.css('height', 'auto');
                innerH = $inner.height();
                $inner.removeAttr('style');
                $inner.data('height', innerH);
            }

            if ($parent.hasClass('js_show')) {
                $parent.removeClass('js_show');
            } else {
                $parent.siblings().removeClass('js_show');
                $parent.addClass('js_show');
                if (this.offsetTop + this.offsetHeight + innerH > $page.scrollTop() + winH) {
                    var scrollTop = this.offsetTop + this.offsetHeight + innerH - winH + categorySpace;

                    if (scrollTop > this.offsetTop) {
                        scrollTop = this.offsetTop - categorySpace;
                    }

                    $page.scrollTop(scrollTop);
                }
            }
        });
    }
};
export default pageManager;